package cn.zysuyuan.item.service.impl;

import cn.zysuyuan.common.entity.Constant;
import cn.zysuyuan.common.entity.PermissionConstants;
import cn.zysuyuan.common.exception.CommonException;
import cn.zysuyuan.common.utils.BeanMapUtils;
import cn.zysuyuan.common.utils.UUIDUtil;
import cn.zysuyuan.item.entity.Permission;
import cn.zysuyuan.item.entity.PermissionApi;
import cn.zysuyuan.item.entity.PermissionMenu;
import cn.zysuyuan.item.entity.PermissionPoint;
import cn.zysuyuan.item.mapper.*;
import cn.zysuyuan.item.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author flypig
 * @version 1.0
 * @date 2020-03-17 11:08
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private PermissionApiMapper permissionApiMapper;

    @Autowired
    private PermissionMenuMapper permissionMenuMapper;

    @Autowired
    private PermissionPointMapper permissionPointMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 保存权限
     * @param map
     */
    @Override
    public void save(Map<String, Object> map) throws Exception {
        // 设置权限的主键
        String id = UUIDUtil.uuid();
        // 通过map构造permission对象
//        if (map.containsKey("porder")) {
//            String porder = (String)map.get("porder");
//            map.put("porder",Integer.parseInt(porder));
//        }
        Permission perm = BeanMapUtils.mapToBean(map,Permission.class);
        perm.setId(id);
        // 根据类型构造不同的资源对象（菜单、按钮、api）
        int type = perm.getType();
        switch (type) {
            case PermissionConstants.PY_MENU:
//                String menuOrder = (String)map.get("menuOrder");
//                map.put("menuOrder",Integer.parseInt(menuOrder));
                PermissionMenu menu = BeanMapUtils.mapToBean(map,PermissionMenu.class);
                menu.setId(id);
                permissionMenuMapper.save(menu);
                break;
            case PermissionConstants.PY_POINT:
//                String pointStatus = (String)map.get("pointStatus");
//                map.put("pointStatus",Integer.parseInt(pointStatus));
                PermissionPoint point = BeanMapUtils.mapToBean(map,PermissionPoint.class);
                point.setId(id);
                permissionPointMapper.save(point);
                break;
            case PermissionConstants.PY_API:
                PermissionApi api = BeanMapUtils.mapToBean(map,PermissionApi.class);
                api.setId(id);
                permissionApiMapper.save(api);
                break;
            default:
                throw new CommonException(Constant.ERROR_CODE);
        }
        // 3、保存权限
        permissionMapper.save(perm);
    }

    @Override
    public List<Permission> findAll(Map<String, Object> map) {

        List<Permission> list = permissionMapper.findAll(map);
        return list;
    }

    @Override
    public Map<String, Object> findById(String id) {
        Permission perm = permissionMapper.findById(id);
        int type = perm.getType();
        Object object = null;
        try {
            if (type == PermissionConstants.PY_MENU) {
                object = permissionMenuMapper.findById(id);
            } else if (type == PermissionConstants.PY_POINT) {
                object = permissionPointMapper.findById(id);
            } else if (type == PermissionConstants.PY_API) {
                object = permissionApiMapper.findById(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String,Object> map = BeanMapUtils.beanToMap(object);

        map.put("name",perm.getName());
        map.put("type",perm.getType());
        map.put("code",perm.getCode());
        map.put("description",perm.getDescription());
        map.put("pid",perm.getPid());
//        map.put("enVisible",perm.getEnVisible());

        Map<String,Object> maps = new HashMap<>();
        maps.put("data",map);
        return maps;
    }

    @Override
    public void update(Map<String, Object> map) throws Exception {
//        if (map.containsKey("porder")) {
//            String porder = (String)map.get("porder");
//            map.put("porder",Integer.parseInt(porder));
//        }
        System.out.println(map.get("porder"));
        Permission perm = BeanMapUtils.mapToBean(map,Permission.class);
//        Permission permission = permissionMapper.findById(perm.getId());
//        permission.setName(perm.getName());
//        permission.setCode(perm.getCode());
//        permission.setDescription(perm.getDescription());
//        permission.setPorder(perm.getPorder());
        // 根据类型构造不同的资源
        int type = perm.getType();
        switch (type) {
            case PermissionConstants.PY_MENU:
//                String menuOrder = (String)map.get("menuOrder");
//                map.put("menuOrder",Integer.parseInt(menuOrder));
                PermissionMenu menu = BeanMapUtils.mapToBean(map,PermissionMenu.class);
                menu.setId(perm.getId());
                permissionMenuMapper.update(menu);
                break;
            case PermissionConstants.PY_POINT:
//                String pointStatus = (String)map.get("pointStatus");
//                map.put("pointStatus",Integer.parseInt(pointStatus));
                PermissionPoint point = BeanMapUtils.mapToBean(map,PermissionPoint.class);
                point.setId(perm.getId());
                permissionPointMapper.update(point);
                break;
            case PermissionConstants.PY_API:
                PermissionApi api = BeanMapUtils.mapToBean(map,PermissionApi.class);
                api.setId(perm.getId());
                permissionApiMapper.update(api);
                break;
            default:
                throw new CommonException(Constant.ERROR_CODE);
        }
        permissionMapper.update(perm);
    }

    @Override
    public void deleteById(String id) throws CommonException {
        // 找出该权限所有的子权限，遍历删除
        List<String> ids = permissionMapper.findSonPermission(id);
        for (int i = 0; i < ids.size(); i++) {
            // 判断按钮权限是否还有api子权限
            List<String> list = permissionMapper.findSonPermission(ids.get(i));
            ids.addAll(list);
        }
        ids.add(id);
        for (int i = 0; i < ids.size(); i++) {
            Permission permission = permissionMapper.findById(ids.get(i));
            permissionMapper.deleteById(ids.get(i));
            // 删除权限管理表中有关此权限的所有数据
            roleMapper.deleteByPermId(ids.get(i));
            int type = permission.getType();
            switch (type) {
                case PermissionConstants.PY_MENU:
                    permissionMenuMapper.deleteById(ids.get(i));
                    break;
                case PermissionConstants.PY_POINT:
                    permissionPointMapper.deleteById(ids.get(i));
                    break;
                case PermissionConstants.PY_API:
                    permissionApiMapper.deleteById(ids.get(i));
                    break;
                default:
                    throw new CommonException(Constant.ERROR_CODE);
            }
        }

    }


}
